/**
 * @program: data_structures_algorithms
 * @description: 用单链表模拟栈
 * @author: lld
 * @create: 2020/08/17 17:30
 **/
public class LinkedListStackDemo {
    public static void main(String[] args) {
        LinkedListStack listStack=new LinkedListStack();
        listStack.list();

        listStack.push(new Node(1));
        listStack.push(new Node(3));
        listStack.push(new Node(2));
        listStack.push(new Node(4));
        listStack.list();

        listStack.pop();
        listStack.list();
    }
}
class LinkedListStack{
    private Node top=new Node(-1);
    //    判断栈空
    public boolean isEmpty(){
        return top.next==null;
    }
    // 入栈
    public void push(Node node){
//        Node temp=top;//辅助指针
        if(top.next==null){//第一个入栈
            top.next=node;
            return;
        }else {
            Node cur=top.next;
            top.next=node;
            node.next=cur;
        }
    }
//    出栈
    public void pop(){
        System.out.println("出栈的是："+top.next.no);
        top=top.next;
    }
//    遍历
    public void list(){
        if(isEmpty()){
            System.out.println("栈空");
            return;
        }
        Node temp=top;
        while (temp.next!=null){
            System.out.println(temp.next.no);
            temp=temp.next;
        }
    }
}
class Node{
    int no;
    Node next;

    public Node(int no) {
        this.no = no;
    }

    @Override
    public String toString() {
        return "Node{" +
                "no=" + no +
                '}';
    }

}
